package leetcode.weekly.week291;

import leetcode.IDebug;

public class Solution4 implements IDebug {

	public long appealSum(String s) {
		char[] cs = s.toCharArray();
		int len = s.length();
		long ans = 0;
		for (int i = 1; i <= len; i++) {
			ans += this.find(cs, i);
		}
		return ans;
	}

	private long find(char[] cs, int index) {
		int[] hash = new int[26];
		int p = 0;
		long cnt = 0;
		while (p < index) {
			int cur = cs[p] - 'a';
			if (hash[cur] == 0) {
				cnt++;
			}
			hash[cur]++;
			p++;
		}
		long ans = cnt;
		while (p < cs.length) {
			int cur = cs[p] - 'a';
			if (hash[cur]++ == 0) {
				cnt++;
			}
			int sub = cs[p - index] - 'a';
			if (--hash[sub] == 0) {
				cnt--;
			}
			ans += cnt;
			p++;
		}
		return ans;
	}

	@Override
	public void debug4() {
		long ans = this.appealSum("abb");
		System.out.println(ans);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution4 so = new Solution4();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
